package com.antis.olsl.magic.profit;

import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.antis.olsl.arouterlib.MagicRoutePath;
import com.antis.olsl.base.BaseActivity;
import com.antis.olsl.magic.R;
import com.antis.olsl.magic.databinding.ActivityLevel5ProfitBinding;
import com.antis.olsl.magic.profit.cash.CashFlowFragment;
import com.antis.olsl.magic.profit.cost.CostAnalysisFragment;

/**
 * 五级毛利
 *
 * @author hhh
 */
@Route(path = MagicRoutePath.ACTIVITY_LEVEL5_PROFIT)
public class Level5ProfitActivity extends BaseActivity<ActivityLevel5ProfitBinding> implements View.OnClickListener {
    public static final int CASH_FLOW_TYPE = 1;
    public static final int COST_ANALYSIS_TYPE = 2;
    public int currentFragmentType = -1;
    private FragmentManager fragmentManager;
    private CashFlowFragment mCashFlowFragment;
    private CostAnalysisFragment mCostAnalysisFragment;

    @Override
    protected ActivityLevel5ProfitBinding getViewBinding() {
        return ActivityLevel5ProfitBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    protected void initViews() {
        viewBinding.tvCashFlow.setSelected(true);
        viewBinding.tvCostAnalysis.setSelected(false);
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        Fragment mainFragment = fragmentManager.findFragmentByTag("message");
        if (mainFragment != null) {
            transaction.replace(R.id.fl_content, mainFragment);
            transaction.commit();
        } else {
            loadFragment(CASH_FLOW_TYPE);
        }

        viewBinding.tvCashFlow.setOnClickListener(this);
        viewBinding.tvCostAnalysis.setOnClickListener(this);
        viewBinding.imgBack.setOnClickListener(this);
    }

    private void loadFragment(int type) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (type == CASH_FLOW_TYPE) {
            if (mCashFlowFragment == null) {
                mCashFlowFragment = new CashFlowFragment();
                transaction.add(R.id.fl_content, mCashFlowFragment, "资金周转");
            } else {
                transaction.show(mCashFlowFragment);
            }
            if (null != mCostAnalysisFragment) {
                transaction.hide(mCostAnalysisFragment);
            }
            currentFragmentType = COST_ANALYSIS_TYPE;
        } else {
            if (mCostAnalysisFragment == null) {
                mCostAnalysisFragment = new CostAnalysisFragment();
                transaction.add(R.id.fl_content, mCostAnalysisFragment, "成本分析");
            } else {
                transaction.show(mCostAnalysisFragment);
            }
            if (null != mCashFlowFragment) {
                transaction.hide(mCashFlowFragment);
            }
            currentFragmentType = CASH_FLOW_TYPE;
        }
        transaction.commitAllowingStateLoss();
    }

    @Override
    protected void initData() throws IllegalAccessException {

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_cash_flow) {
            viewBinding.tvCashFlow.setSelected(true);
            viewBinding.tvCostAnalysis.setSelected(false);
            switchFragment(CASH_FLOW_TYPE);
        } else if (id == R.id.tv_cost_analysis) {
            viewBinding.tvCostAnalysis.setSelected(true);
            viewBinding.tvCashFlow.setSelected(false);
            switchFragment(COST_ANALYSIS_TYPE);
        } else if (id == R.id.img_back) {
            finish();
        }
    }

    private void switchFragment(int type) {
        switch (type) {
            case CASH_FLOW_TYPE:
                loadFragment(CASH_FLOW_TYPE);
                break;
            case COST_ANALYSIS_TYPE:
                loadFragment(COST_ANALYSIS_TYPE);
                break;
            default:
                break;
        }
    }
}
